<template>
  <div class="main-container" v-if="!isLoginPage">
    <!-- 页面标题区域 -->
    <div class="page-header">
      <div class="header-content">
        <h1 class="main-title">实验室环境安全监控系统</h1>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="content-area">
      <!-- 侧边栏控制按钮 -->
      <el-radio-group v-model="isCollapse" style="margin-bottom: 20px">
        <el-radio-button :value="false">展开</el-radio-button>
        <el-radio-button :value="true">收起</el-radio-button>
      </el-radio-group>
      <div class="layout-content">
        <!-- 侧边栏菜单 -->
        <el-menu :default-active="activeMenu" class="el-menu-vertical-demo" :collapse="isCollapse" @open="handleOpen"
          @close="handleClose">
          <el-sub-menu index="1">
            <template #title>
              <el-icon>
                <icon-menu />
              </el-icon>
              <span>控制台</span>
            </template>
            <el-menu-item-group>
              <!-- 为数据统计添加点击事件 -->
              <el-menu-item index="1-1" @click="goToDataStatistics">
                数据统计
              </el-menu-item>
              <el-menu-item index="1-2" @click="goToDataDisplay">
                数据展示
              </el-menu-item>
              <el-menu-item index="1-3" @click="goToControlPanel">控制面板</el-menu-item>
              <el-menu-item index="1-4" @click="goToDataAnalysis">数据分析</el-menu-item>
            </el-menu-item-group>
          </el-sub-menu>

          <!-- 设置菜单项 -->
          <el-menu-item index="2" @click="goToSetting">
            <el-icon>
              <setting />
            </el-icon>
            <template #title>设置</template>
          </el-menu-item>
        </el-menu>

        <!-- 内容显示区域 -->
        <div class="page-content">
          <router-view></router-view>
        </div>
      </div>
    </div>
  </div>
  <div v-else>
    <router-view></router-view>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'
import {
  Menu as IconMenu,
  Setting,
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()  // 获取当前路由信息
const store = useStore()
const isCollapse = ref(true)

const activeIndex = ref('1')

// 当前时间
const currentTime = ref('')

// 更新时间
const updateCurrentTime = () => {
  const now = new Date()
  currentTime.value = now.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: false
  })
}

// 时间更新定时器
let timeTimer = null

// 动态设置激活的菜单项
const activeMenu = computed(() => {
  if (route.path === '/data-statistics') return '1-1'
  if (route.path === '/data-display') return '1-2'
  if (route.path === '/setting') return '2'
  return '1'
})

const isLoginPage = computed(() => route.path === '/login')
const isAuthenticated = computed(() => store.getters.isAuthenticated)
const username = computed(() => store.getters.username)
// 头像已移除，仅显示用户名

const handleOpen = (key, keyPath) => {
  console.log(key, keyPath)
}

const handleClose = (key, keyPath) => {
  console.log(key, keyPath)
}

// 跳转到数据统计页面
const goToDataStatistics = () => {
  router.push('/data-statistics')
}

// 跳转到数据展示页面
const goToDataDisplay = () => {
  router.push('/data-display')
}

// 跳转到设置页面
const goToSetting = () => {
  router.push('/setting')
}

// 跳转到控制面板
const goToControlPanel = () => {
  router.push('/control-panel')
}

// 跳转到数据分析
const goToDataAnalysis = () => {
  router.push('/data-analysis')
}

const handleLogout = () => {
  store.dispatch('logout')
  router.replace({ path: '/login' })
}

// 组件挂载时启动时间更新
onMounted(() => {
  updateCurrentTime()
  timeTimer = setInterval(updateCurrentTime, 1000)
})

// 组件卸载时清理定时器
onUnmounted(() => {
  if (timeTimer) {
    clearInterval(timeTimer)
    timeTimer = null
  }
})
</script>

<style>
.main-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.page-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 20px 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  flex-direction: column;
  gap: 15px;
}

.main-title {
  margin: 0;
  font-size: 28px;
  font-weight: 600;
  background: linear-gradient(135deg, #fff 0%, #e0e6ff 100%);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-info {
  display: flex;
  align-items: center;
  gap: 30px;
}

.system-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  padding: 6px 12px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  backdrop-filter: blur(10px);
}

.system-status::before {
  content: '';
  width: 8px;
  height: 8px;
  background: #4ade80;
  border-radius: 50%;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

.current-time {
  font-size: 14px;
  font-weight: 500;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  backdrop-filter: blur(10px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header {
    padding: 15px 0;
  }

  .main-title {
    font-size: 24px;
  }

  .header-info {
    gap: 15px;
  }

  .system-status,
  .current-time {
    font-size: 12px;
    padding: 6px 10px;
  }
}

@media (max-width: 480px) {
  .main-title {
    font-size: 20px;
  }

  .header-info {
    gap: 10px;
  }

  .system-status,
  .current-time {
    font-size: 11px;
    padding: 4px 8px;
  }
}

.content-area {
  flex: 1;
  padding: 20px;
}

.layout-content {
  display: flex;
  height: 100%;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.page-content {
  flex: 1;
  padding: 20px;
  background: #f5f5f5;
  overflow: auto;
}

.logo {
  width: 55px;
  height: 55px;
  background: white;
  margin-left: auto;
  margin-right: 10px;
}

.right-content {
  margin-left: auto;
  display: flex;
  align-items: center;
  padding: 0 20px;
}
</style>
